import type { Point } from 'geojson'
import type { LngLat } from '@/geom/great-circle'
import { keyGen } from '@/common'
import { type MapFeatures, type SymbolFeature } from '@/map'
import { type AuxGraphic } from '../core'
import { auxGraphicProperties } from '../core'

const nameGen = keyGen('辅助点#')

export function nextName(): string {
  return nameGen.next()
}

export interface AuxPoint extends AuxGraphic {
  type: 'Point'
  point: LngLat
}

export function toPoint(ap: { point: LngLat }): Point {
  const loc = ap.point
  return {
    type: 'Point',
    coordinates: [loc.lng, loc.lat]
  }
}

export function toFeature(ap: AuxPoint, g: Point[]): MapFeatures {
  const f: SymbolFeature = {
    type: 'Feature',
    geometry: g.length > 0 ? g[0] : toPoint(ap),
    properties: auxGraphicProperties.symbol(ap.name)
  }
  return { symbol: [f] }
}
